package com.boyuan.huijiaoyu;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.LinearLayout;

import com.boyuan.huijiaoyu.R;
import com.magicsoft.weitown.ui.DragImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;


public class ImageViewZoomActivity extends BaseActivity implements OnClickListener {
//	private int window_width, window_height;// 控件宽度
	private DragImageView dragImageView;// 自定义控件
	private int state_height;// 状态栏的高度

	private ViewTreeObserver viewTreeObserver;
	private String imageUrl = "";
	private DisplayImageOptions options;
	private LinearLayout layout;
	 int window_width;
	 int window_height;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.imageviewzoom_activity);
		getForIntent();
		prepareView();

	}

	private void prepareView() {
		/** 获取可見区域高度 **/
		Display display = getWindowManager().getDefaultDisplay();

		  window_width = display.getWidth();
		  window_height = display.getHeight();

		dragImageView = (DragImageView) findViewById(R.id.div_main);

		layout = (LinearLayout) findViewById(R.id.linearlayout);
		layout.setOnClickListener(this);
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.placeholder)
		.showImageForEmptyUri(R.drawable.placeholder)
		.showImageOnFail(R.drawable.placeholder)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		//.displayer(new RoundedBitmapDisplayer(20))
		.build();
	
		if (imageUrl != null && imageUrl.trim().length() > 0) {
			imageLoader.displayImage(imageUrl, dragImageView,options,new ImageLoadingListener());
		}
//		dragImageView.setLayoutParams(new RelativeLayout.LayoutParams(
//				window_width, window_width));
//		dragImageView.setImageBitMapWh(window_width, window_height);
		dragImageView.setmActivity(this);// 注入Activity.
		/** 测量状态栏高度 **/
//		viewTreeObserver = dragImageView.getViewTreeObserver();
//		viewTreeObserver
//				.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//
//					@Override
//					public void onGlobalLayout() {
//						if (state_height == 0) {
//							// 获取状况栏高度
//							Rect frame = new Rect();
//							getWindow().getDecorView()
//									.getWindowVisibleDisplayFrame(frame);
//							state_height = frame.top;
//							dragImageView.setScreen_H(window_height
//									- state_height);
//							dragImageView.setScreen_W(window_width);
//						}
//
//					}
//				});
	}
	//ImageLoading回调
		private class ImageLoadingListener extends SimpleImageLoadingListener {
			@Override
			public void onLoadingComplete(String imageUri, View view,
					Bitmap loadedImage) {
				// TODO Auto-generated method stub
				
				if (loadedImage != null) {

					float bmHeight = loadedImage.getHeight();
					float bmWidth = loadedImage.getWidth();
					float ivHeight = bmHeight * (window_width / bmWidth);
					dragImageView.setLayoutParams(new LinearLayout.LayoutParams(
							window_width, (int) ivHeight));
					dragImageView.setImageBitMapWh(window_width, window_height);
					dragImageView.setmActivity(ImageViewZoomActivity.this);// 注入Activity.
//					dragImageView.setScaleType(ScaleType.FIT_XY);
					dragImageView.setImageBitmap(loadedImage);
					
				}

			}
		}

	private void getForIntent() {
		Intent intent = getIntent();
		imageUrl = intent.getStringExtra("imageurl");
	}



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layout:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void backClicked() {
		// TODO Auto-generated method stub
		finish();
	}

}